package cn.mxb.logic.action.mxb;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysUser;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.SysZu;
import cn.mxb.logic.entity.MxbBingli;
import cn.mxb.logic.entity.MxbBxRegister;
import cn.mxb.logic.entity.MxbPolicy;
import cn.mxb.logic.entity.MxbPrsinfo;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysUserService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.ISysZuService;
import unit.IdGenerator;
import unit.MyTime;
import cn.mxb.logic.service.IMxbBingliService;
import cn.mxb.logic.service.IMxbBxRegisterService;
import cn.mxb.logic.service.IMxbPolicyService;
import cn.mxb.logic.service.IMxbPrsinfoService;

@SuppressWarnings({ "serial", "rawtypes" })
public class MxbBxRegisterAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/mxb/mxbBxRegisterList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/mxb/mxbBxRegisterAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/mxb/mxbBxRegisterUpdate.jsp";
	protected static final String MXBCARDREGISTRATIONADDJSP = "/WEB-INF/page/web/mxb/mxbBxRegisterregistrationAdd.jsp";
	
	
	private MxbBxRegister mxbBxRegister;
	
	@Autowired
	private IMxbBxRegisterService mxbBxRegisterService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysZuService sysZuService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IMxbPolicyService mxbPolicyService;
	@Autowired
	private IMxbPrsinfoService mxbPrsinfoService;
	@Autowired
	private IMxbBingliService mxbBingliService;
	
	
	private Result<MxbBxRegister> pageResult;
	
	private List<MxbBxRegister> mxbBxRegisterAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	private List<SysZu> sysZuAllList; 
	private List<SysUser> sysUserList; 
	private List<MxbPolicy> mxbPolicicyAllList;
	private List<MxbBingli> mxbBingliBxList;
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();    //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();     //镇名
	private Map<Integer,String> villageNameMap = new HashMap<Integer, String>();  //村名
	private Map<Integer,String> zUidMap = new HashMap<Integer, String>(); 
	private Map<String,String> userNameMap = new HashMap<String, String>(); 
	private Map<String,String> mxbNameMap = new HashMap<String, String>(); 
	private Map<String,String> bxUserNameMap =  new HashMap<String, String>(); 
	private String familyNameY; //参合家庭姓名搜索关键字
	private String userRealNameY;
	private String caheDateY;
	private String bxAllMoneyY;
	private String bingliMoneyY;
	/**
	 * 打开报销界面
	 * @return
	 */
	public String openbx(){
		Double bingliMoney = 0.0;
		if(mxbBxRegister.getId() !=null && !"".equals(mxbBxRegister.getId()) ){
			if(mxbBxRegister.getBxUserId() !=null && !"".equals(mxbBxRegister.getBxUserId())){
				//报销人员信息
				SysUser sysUserBx = new SysUser();
				sysUserBx = sysUserService.findByProperty("userId", mxbBxRegister.getBxUserId());
				userRealNameY = sysUserBx.getUserRealName();
				mxbBxRegister.setBxUserId(sysUserBx.getUserId());
				mxbBxRegister.setBxDate(MyTime.getNowDate()+"");
				//报销人员参合信息
				MxbPrsinfo mxbPrsinfo = new MxbPrsinfo();
				mxbPrsinfo = mxbPrsinfoService.findByProperty("prsinfoId", mxbBxRegister.getBxUserId());
			    caheDateY =  mxbPolicyService.findByProperty("policyId", mxbPrsinfo.getPolicyId()).getPolicyDate()+"";
			    bxAllMoneyY = mxbPrsinfo.getBxAllMoney()+"";
			    mxbBingliBxList = mxbBingliService.findListByPropertyTwo("userId", mxbPrsinfo.getPrsinfoId(), "bxState", "1");
			    for(int i=0;i<mxbBingliBxList.size();i++){
			    	bingliMoney = bingliMoney + mxbBingliBxList.get(i).getBingliMoney();
			    	mxbBxRegister.setBxMoney(bingliMoney);
			    }
			}
		}
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	
	
	/**
	 * 报销
	 * @throws Exception 
	 * @return_type: String
	 */
	public String bxadd() throws Exception{
		System.out.println("bxadd");
		/*IdGenerator id = new IdGenerator(1023);
		mxbBxRegister.setFamilyId((id.getNextId()).intValue());*/
		
		return openList();		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = mxbBxRegisterService.find(mxbBxRegister, getPage(), getRow());
			for(MxbBxRegister mxbBxRegister : pageResult.getData()){
				if(mxbBxRegister.getBxUserId() !=null && !"".equals(mxbBxRegister.getBxUserId())){
					bxUserNameMap.put(mxbBxRegister.getBxUserId(),sysUserService.findByProperty("userId", mxbBxRegister.getBxUserId()).getUserRealName());
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		mxbBxRegisterService.delete(mxbBxRegister.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		mxbBxRegister =  mxbBxRegisterService.findByProperty("id",mxbBxRegister.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		mxbBxRegisterService.add(mxbBxRegister);
		mxbBxRegister = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return mxbBxRegister;
	}
	@Override
	public void prepare() throws Exception {
		if(mxbBxRegister ==null){
			mxbBxRegister = new MxbBxRegister();
		}
	}


	public MxbBxRegister getMxbBxRegister() {
		return mxbBxRegister;
	}

	public void setMxbBxRegister(MxbBxRegister mxbBxRegister) {
		this.mxbBxRegister = mxbBxRegister;
	}

	public Result<MxbBxRegister> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<MxbBxRegister> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	public Map<Integer, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<Integer, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<MxbBxRegister> getMxbBxRegisterAllList() {
		return mxbBxRegisterAllList;
	}

	public void setMxbBxRegisterAllList(List<MxbBxRegister> mxbBxRegisterAllList) {
		this.mxbBxRegisterAllList = mxbBxRegisterAllList;
	}

	public List<SysZu> getSysZuAllList() {
		return sysZuAllList;
	}

	public void setSysZuAllList(List<SysZu> sysZuAllList) {
		this.sysZuAllList = sysZuAllList;
	}

	public Map<Integer, String> getzUidMap() {
		return zUidMap;
	}

	public void setzUidMap(Map<Integer, String> zUidMap) {
		this.zUidMap = zUidMap;
	}

	public String getFamilyNameY() {
		return familyNameY;
	}

	public void setFamilyNameY(String familyNameY) {
		this.familyNameY = familyNameY;
	}

	public Map<String, String> getUserNameMap() {
		return userNameMap;
	}

	public void setUserNameMap(Map<String, String> userNameMap) {
		this.userNameMap = userNameMap;
	}

	public Map<String, String> getMxbNameMap() {
		return mxbNameMap;
	}

	public void setMxbNameMap(Map<String, String> mxbNameMap) {
		this.mxbNameMap = mxbNameMap;
	}

	public List<SysUser> getSysUserList() {
		return sysUserList;
	}

	public void setSysUserList(List<SysUser> sysUserList) {
		this.sysUserList = sysUserList;
	}

	public List<MxbPolicy> getMxbPolicicyAllList() {
		return mxbPolicicyAllList;
	}

	public void setMxbPolicicyAllList(List<MxbPolicy> mxbPolicicyAllList) {
		this.mxbPolicicyAllList = mxbPolicicyAllList;
	}

	public String getUserRealNameY() {
		return userRealNameY;
	}

	public void setUserRealNameY(String userRealNameY) {
		this.userRealNameY = userRealNameY;
	}

	public String getCaheDateY() {
		return caheDateY;
	}

	public void setCaheDateY(String caheDateY) {
		this.caheDateY = caheDateY;
	}

	public String getBxAllMoneyY() {
		return bxAllMoneyY;
	}

	public void setBxAllMoneyY(String bxAllMoneyY) {
		this.bxAllMoneyY = bxAllMoneyY;
	}



	public Map<String, String> getBxUserNameMap() {
		return bxUserNameMap;
	}



	public void setBxUserNameMap(Map<String, String> bxUserNameMap) {
		this.bxUserNameMap = bxUserNameMap;
	}

}
